package com.edu.view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.edu.controller.ChessController;
import com.edu.model.Board;
import com.edu.model.Piece;
import com.edu.util.ChessHelper;

/**
 * 
 * 视图层
 * 
 * @author zhu
 * @version 0.1
 */
public class ChessView extends JFrame {

	private Board board;

	public static final int VIEW_WIDTH = 500, VIEW_HEIGHT = 500;
	public static final int SIDE_LENGTH = 40;
	public static final int PIECE_R = 18;
	public static final int SY_COE = 68, SX_COE = 68;
	public static final int SX_OFFSET = 50, SY_OFFSET = 15;
	// private Map<String, JLabel> pieceObjects = new HashMap<String, JLabel>();

	private String selectedPieceKey;
	
	private ChessController controller;

	public void setController(ChessController controller) {
		this.controller = controller;
	}

	public String getSelectedPieceKey() {
		return selectedPieceKey;
	}

	public void setSelectedPieceKey(String selectedPieceKey) {
		this.selectedPieceKey = selectedPieceKey;
	}

	// 初始化视图
	public void init(final Board board) {
		this.board = board;
		this.setTitle("中国象棋");
		this.setSize(VIEW_WIDTH, VIEW_HEIGHT);

		JPanel panel = new ChessPanel();
		this.setContentPane(panel);
		pack();
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(VIEW_WIDTH, VIEW_HEIGHT);
	}

	public void render() {
		
	}
	
	
	private class ChessPanel extends JPanel {
		
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g;
			// 抗锯齿
			RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.addRenderingHints(hints);

			Map<String, Piece> pieces = board.getPieces();
			initBoard(g2d);

			initPiece(g2d, pieces);

		}

		private void initPiece(Graphics2D g2d, Map<String, Piece> pieces) {
			// 画棋子
			for (Piece piece : pieces.values()) {		
					ChessHelper.drawPiece(g2d, piece, selectedPieceKey , PIECE_R , SIDE_LENGTH);
			}
		}

		private void initBoard(Graphics2D g2d) {
			// 画10条横线
			for (int i = 1; i <= 10; i++) {
				ChessHelper.drawLine(g2d, new Point(1 * SIDE_LENGTH, i * SIDE_LENGTH), new Point(9 * SIDE_LENGTH, i * SIDE_LENGTH));
			}

			// 画9条竖线
			for (int i = 1; i <= 9; i++) {
				if (i == 1 || i == 9) {
					ChessHelper.drawLine(g2d, new Point(i * SIDE_LENGTH, 1 * SIDE_LENGTH), new Point(i * SIDE_LENGTH, 10 * SIDE_LENGTH));
				} else {
					ChessHelper.drawLine(g2d, new Point(i * SIDE_LENGTH, 1 * SIDE_LENGTH), new Point(i * SIDE_LENGTH, 5 * SIDE_LENGTH));
					ChessHelper.drawLine(g2d, new Point(i * SIDE_LENGTH, 6 * SIDE_LENGTH), new Point(i * SIDE_LENGTH, 10 * SIDE_LENGTH));
				}
			}
			// 画9宫格
			ChessHelper.drawLine(g2d, new Point(4 * SIDE_LENGTH, 1 * SIDE_LENGTH), new Point(6 * SIDE_LENGTH, 3 * SIDE_LENGTH));
			ChessHelper.drawLine(g2d, new Point(6 * SIDE_LENGTH, 1 * SIDE_LENGTH), new Point(4 * SIDE_LENGTH, 3 * SIDE_LENGTH));
			ChessHelper.drawLine(g2d, new Point(4 * SIDE_LENGTH, 8 * SIDE_LENGTH), new Point(6 * SIDE_LENGTH, 10 * SIDE_LENGTH));
			ChessHelper.drawLine(g2d, new Point(6 * SIDE_LENGTH, 8 * SIDE_LENGTH), new Point(4 * SIDE_LENGTH, 10 * SIDE_LENGTH));
		}
	}
}
